package com.cms.CRUD;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cms.exception.WrongIdException;
import com.cms.factory.MethodFactory;
import com.cms.model.Customer;

public class FileOperate {


	/*
	 * 实现单例
	 */
	private static FileOperate fileOperate = new FileOperate();

	private FileOperate() {

	}

	public static FileOperate getInstance() {
		return fileOperate;
	}

	//静态属性
	private static File file;
	private static List<Customer> customers = new ArrayList<Customer>();

	//读取info.dat
	@SuppressWarnings("unchecked")
	public void start() {
		file = new File("info.dat");
		if (!file.exists()) {
			try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
				file.createNewFile();
				List<Customer> list = new ArrayList<Customer>();
				oos.writeObject(list);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {

			customers = (List<Customer>) ois.readObject();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	//存储info.dat数据
	public void save() {
		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
			oos.writeObject(customers);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void insert(Customer customer) {
		start();
		customers.add(customer);
		save();
	}

	public void delete(String id) throws WrongIdException {
		start();
		boolean flag=false;
		Iterator<Customer> iterator = customers.iterator();
		while (iterator.hasNext()) {
			if (iterator.next().getId().equals(id)) {
				iterator.remove();
				flag=true;
			}
		}
		if(!flag) {
			throw new WrongIdException("不存在编号为"+id+"的客户");
		}
		save();
	}

	public void update(Customer customer) throws WrongIdException {
		start();
		boolean flag=false;
		for (int i = 0; i < customers.size(); i++) {
			if (customers.get(i).getId().equals(customer.getId())) {
				customers.get(i).setName(customer.getName());
				customers.get(i).setAddress(customer.getAddress());
				flag=true;
			}
		}
		if(!flag) {
			throw new WrongIdException("不存在编号为"+customer.getId()+"的客户");
		}
		save();
	}

	public List<Customer> searchByCondition(String sid, String value) throws IllegalAccessException,
			IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		start();
		List<Customer> list = new ArrayList<Customer>();

		for (Customer c : customers) {
			if (MethodFactory.getMethod(sid).invoke(c).equals(value))
				list.add(c);

		}
		save();
		return list;
	}

	//获得全部各户信息
	public List<Customer> showAll() {
		start();
		save();

		return customers;
	}

}
